



//
//  ZYTeachersAccountNumberController.m
//  优学荟管理系统
//
//  Created by edz on 2017/12/8.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYTeachersAccountNumberController.h"
#import "ZYTeachersAccountNumberModel.h"
#import "ZYTeachersAccountNumberCell.h"
#define kCell @"cell"
#import "OrdinaryTeacherAddVC.h"

@interface ZYTeachersAccountNumberController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UIView *allView;
@end

@implementation ZYTeachersAccountNumberController


-(NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
    
}



- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(0, 0, 0)}];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"老师账号";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(0, 0, 0)}];
    self.view.backgroundColor = ColorRGB(247, 245, 245);
    
    UIBarButtonItem *liftItem = [UIBarButtonItem itemWithTargat:self action:@selector(itemAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
    
    self.navigationItem.leftBarButtonItem =liftItem;
    [self setNetWork];
    [self setUpSuperView];
    
    
    
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(0, 0, 100, 30);
    [button setTitle:@"添加账号" forState:(UIControlStateNormal)];
    [button setTitleColor:ColorRGB(0, 0, 0) forState:(UIControlStateNormal)];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button addTarget:self action:@selector(rightItemAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    

    
}


    
    


- (void)itemAction{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)rightItemAction{
    
    
     [self.navigationController pushViewController:[OrdinaryTeacherAddVC new] animated:YES];
    
    
}






- (void)setNetWork{
   
    NSString *url = [NSString stringWithFormat:@"lesson.do?action=getTeacherByOid&oid=%@",[AccountTool shareAccountTool].currentAccount.oid];
    
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            [self.dataSource removeAllObjects];
            
            
            NSArray *t = [Obj valueForKey:@"t"];
            
            for (NSDictionary *dict  in t) {
                
                ZYTeachersAccountNumberModel *model = [ZYTeachersAccountNumberModel new];
                
                [model setValuesForKeysWithDictionary:dict];
                
                
                [self.dataSource addObject:model];
                
                
            }
            
            [self.tableView reloadData];
            
            
            
        }
        
        
        
        
        
        
        
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
    
    
    
    
    
    
}
























- (void)setUpSuperView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    self.tableView.dataSource  = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = ColorRGB(247, 245, 245);
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    
    self.tableView.estimatedRowHeight = 200;
    [self.tableView registerClass:[ZYTeachersAccountNumberCell class] forCellReuseIdentifier:kCell];

    self.allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight *30/1334)];
    self.allView.backgroundColor = ColorRGB(247, 245, 245);
    self.tableView.tableHeaderView = self.allView;
    self.tableView.tableFooterView = [UIView new];
    
    
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYTeachersAccountNumberCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    if (self.dataSource.count > indexPath.row) {
        
        ZYTeachersAccountNumberModel *model = self.dataSource[indexPath.row];
        
        [cell assignByZYTeachersAccountNumberModel:model];
        
        
        
    }
    
    [cell.deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.upDateButton addTarget:self action:@selector(upDateButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.callButton addTarget:self action:@selector(callButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



#pragma mark ---- 删除方法的实现
- (void)deleteButtonAction:(UIButton *)sender{
    
    ZYTeachersAccountNumberCell *cell = (ZYTeachersAccountNumberCell *)sender.superview.superview;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYTeachersAccountNumberModel *model = self.dataSource[indexPath.row];
    
    
    
    
    ZYalertController *alert = [ZYalertController alertControllerWithTitle:@"温馨提示" message:@"是否删除?" preferredStyle:(UIAlertControllerStyleAlert)];
    ZYAlertAction *action = [ZYAlertAction actionWithTitle:@"YES" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        if (_dataSource.count > 1) {
            
            
            NSMutableArray *temp = [NSMutableArray arrayWithArray:_dataSource];
            
            [temp removeObjectAtIndex:indexPath.row];
            
            _dataSource = temp;
            
            NSArray *deleteItems = @[indexPath];
            
            [self.tableView deleteRowsAtIndexPaths:deleteItems withRowAnimation:(UITableViewRowAnimationRight)];
            
        }else{
            [_dataSource removeAllObjects];
            [self.tableView reloadData];
        }
        
        
        [self netMoonWorkId:[NSString stringWithFormat:@"%@",model.uid]];
        
        
    }];
    
    ZYAlertAction *actionn = [ZYAlertAction actionWithTitle:@"NO" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    action.textColor = [UIColor blackColor];
    actionn.textColor = [UIColor blackColor];
    [alert addAction:action];
    [alert addAction:actionn];
    [self presentViewController:alert animated:YES completion:nil];
    
    
  
}



#pragma mark--- 修改方法的实现
- (void)upDateButtonAction:(UIButton *)sender{
    
    ZYTeachersAccountNumberCell *cell = (ZYTeachersAccountNumberCell *)sender.superview.superview;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYTeachersAccountNumberModel *model = self.dataSource[indexPath.row];
    
    OrdinaryTeacherAddVC *VC = [[OrdinaryTeacherAddVC alloc] init];
    VC.ID = (NSNumber *)model.uid;
    VC.popBlock = ^{
      
        [self setNetWork];
    };

    
    [self.navigationController pushViewController:VC animated:YES];
    
}

#pragma mark ---打电话方法的实现
- (void)callButtonAction:(UIButton *)sender{
    

    
    ZYTeachersAccountNumberCell *cell = (ZYTeachersAccountNumberCell *)sender.superview.superview;

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYTeachersAccountNumberModel *model = self.dataSource[indexPath.row];


    [self phoneChat:[NSString stringWithFormat:@"%@",model.phone]];

    
    
}


#pragma mark ----phoneChat方法的实现
- (void)phoneChat:(NSString *)phone{
    
    NSString *strs = phone;
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",strs];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
    
}











- (void)netMoonWorkId:(NSString *)Id{
    
   // http://192.168.0.113:8080/StarShine/ws/user.do?action=deleteUser&uid=126400
    
    NSString *url = [NSString stringWithFormat:@"user.do?action=deleteUser&uid=%@",Id];
    
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
    
}





















































@end
